﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.Core.Modelling.Primitives;
using System.Diagnostics;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.Modelling.Components.Constraints
{
    [Serializable]
    class ComponentCreateShprossEndToEndConnectors : IConstraint
    {

        #region IConstraint Members

        public bool Check(IComponent component)
        {
            return false;
        }

        public void Fix(IComponent component)
        {
            if (!(component is Shpross))
                return;

            Shpross shpross = component as Shpross;
            
            this.CreateShprossEndToEndConnector(shpross, shpross.StartConnectedComponent);
            this.CreateShprossEndToEndConnector(shpross, shpross.EndConnectedComponent);
        }

        private void CreateShprossEndToEndConnector(Shpross shpross, IComponent connectedComponent)
        {
            if (connectedComponent is Shpross)
            {
                Shpross connectedShpross = connectedComponent as Shpross;
                EndType shprossEndType = shpross.IsConnectedToEnd(connectedShpross);
                if (shprossEndType != EndType.Both &&
                    shprossEndType != EndType.NotDefined)
                {
                    EndType connectedShprossEndType = connectedShpross.IsConnectedToEnd(shpross);
                    if (connectedShprossEndType != EndType.NotDefined &&
                        connectedShprossEndType != EndType.Both)
                    {
                        if (!ConnectionProvider.Disconnect(shpross, connectedShpross))
                            Debugger.Break();

                        ModelPoint position = null;
                        if (shprossEndType == EndType.Start)
                            position = shpross.StartPoint;
                        else if (shprossEndType == EndType.End)
                            position = shpross.EndPoint;
                        bool isSucceed = false;

                        ShprossEndToEndConnector[] endToEndConnectors = ComponentFactory.CreateShprossEndToEndConnectors(position, 
                            new AngleInterval(new Angle(Math.PI / 5), 
                                              new Angle(2 * Math.PI - Math.PI / 5)));

                        endToEndConnectors[0].UniqueId = shpross.UniqueId;
                        endToEndConnectors[0].AcceptedUniqueIdsToConnect = new List<int>(shpross.AcceptedUniqueIdsToConnect);
                        endToEndConnectors[0].UniqueIdsEnabled = shpross.UniqueIdsEnabled;
                        endToEndConnectors[1].UniqueId = shpross.UniqueId;
                        endToEndConnectors[1].AcceptedUniqueIdsToConnect = new List<int>(shpross.AcceptedUniqueIdsToConnect);
                        endToEndConnectors[1].UniqueIdsEnabled = shpross.UniqueIdsEnabled;
                            
                        isSucceed = ConnectionProvider.Connect(endToEndConnectors[0], shpross, EndType.End, shprossEndType);
                        endToEndConnectors[0].ConnectedEndType = shprossEndType;
                        isSucceed = ConnectionProvider.Connect(endToEndConnectors[1], connectedShpross, EndType.End, connectedShprossEndType);
                        endToEndConnectors[1].ConnectedEndType = connectedShprossEndType;
                        shpross.AddInnerComponent(endToEndConnectors[0]);
                        connectedShpross.AddInnerComponent(endToEndConnectors[1]);
                    }
                }
            }
        }

        private bool enabled = true;   

        public bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
            }
        }

        public ConstraintAction Action
        {
            get { return ConstraintAction.Fix; }
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            ComponentCreateShprossEndToEndConnectors clone = new ComponentCreateShprossEndToEndConnectors();
            clone.Enabled = this.enabled;
            return clone;
        }

        #endregion
    }
}
